import { createRouter, createWebHistory, RouteRecordRaw, RouteRecordNormalized, createWebHashHistory } from 'vue-router';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css';

import { appRoutes } from './routes';
import { DEFAULT_LAYOUT, REDIRECT_MAIN, NOT_FOUND_ROUTE } from './routes/base';
import createRouteGuard from './guard';

NProgress.configure({ showSpinner: false }); // NProgress Configuration

// 基本路由配置
export const BASE_ROUTES: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      requiresAuth: false,
      hideInMenu: true
    },
  },
  REDIRECT_MAIN,
  NOT_FOUND_ROUTE
]

// 默认路由配置
export const CONSTANT_ROUTES: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'dashboard',
    component: DEFAULT_LAYOUT,
    meta: {
      locale: 'menu.dashboard',
      requiresAuth: true,
      icon: 'icon-dashboard',
      order: 0,
    },
    children: [
      {
        path: '/',
        name: 'Workplace',
        component: () => import('@/views/dashboard/workplace/index.vue'),
        meta: {
          locale: 'menu.dashboard.workplace',
          requiresAuth: true,
          roles: ['*'],
        },
      },
      {
        path: 'data',
        name: 'Data',
        component: () => import('@/views/dashboard/data/index.vue'),
        meta: {
          locale: 'menu.dashboard.dataAnalysis', 
          requiresAuth: true,
          roles: ['*'],
        },
      },
    ],
  },
  {
    path: '/user',
    name: 'user',
    component: DEFAULT_LAYOUT,
    meta: {
      locale: 'menu.user',
      icon: 'icon-user',
      requiresAuth: true,
      order: 7,
    },
    children: [
      {
        path: 'setting',
        name: 'Setting',
        component: () => import('@/views/sys/user/setting/index.vue'),
        meta: {
          locale: 'menu.user.setting',
          requiresAuth: true,
          hideInMenu: true,
          roles: ['*'],
        },
      },
    ],
  }
]

// 本地动态路由配置
export const DYNMIC_ROUTES: RouteRecordRaw[] = [
  // 更多的静态路由... Mock数据
  ...appRoutes
];

const router = createRouter({
  history: createWebHistory(),
  routes: BASE_ROUTES.concat(CONSTANT_ROUTES),
  // scrollBehavior() {
  //   return { top: 0 };
  // },
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    }
    return { top: 0 }
  },
});

createRouteGuard(router);

export default router;
